<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">


    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>

    <script src="js/jquery.min.js"></script>
    <script src="js/urlUtils.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">首页</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="login.html">登陆</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <div id = "artListDiv">


            </div>
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="doFirst()">首页</button> 
                <button class="blog-pagnation-item " onclick="doBefore()">上一页</button>  
                <button class="blog-pagnation-item" onclick="doNext()">下一页</button>
                <button class="blog-pagnation-item" onclick="doLast()">末页</button>
                &nbsp;&nbsp;&nbsp;
                当前在第 <span id = "pindex"></span> 页,共 <span id = "psize"></span> 页

            </div>
        </div>


           

        
    </div>

    <script>
        // 前端传递的内容 页码--当前在第几页 每页显示的最大条数-- 前端灵活的控制分页功能
        var psize = 2; // 每页显示条数
        var pindex = 1;
        var totalpapge = 1;



        // 初始化数据
        function init() {
            
            // 1.初始化数据 得到url中的分页参数
            psize = getParamValue("psize");
            if (psize == null) {
                psize = 2;
            }
            pindex = getParamValue("pindex");
            if (pindex == null) {
                pindex = 1;
            }
            jQuery("#pindex").html(pindex);
            // 2/请求后端接口
            jQuery.ajax({
                url : "/art/getlistbypage",
                type : "GET",
                data : {
                    "psize" : psize,
                    "pindex" : pindex
                },
                success : function succ (res) {
                    // 3.将结果展示给用户
                    if (res.code == 200 && res.data != null) {
                         var createHtml = "";
                        if (res.data.list != null && res.data.list.length > 0) {
                            totalpapge = res.data.size;
                            jQuery("#psize").html(totalpapge);
                            
                            var artlist = res.data.list;
                            for (var i = 0; i < res.data.list.length ; i++) {
                                var art = artlist[i];
                                createHtml += '<div class="blog" >';
                                createHtml += '<div class="title">' +art.title + '</div>';
                                createHtml += '<div class="date">' + art.createtime+'</div>';
                                createHtml += '<div class="desc">' + art.content +'</div>';
                                createHtml += '<a href="blog_content.html?aid=' + art.id + '" class="detail">查看全文 &gt;&gt;</a>';
                                createHtml += '</div>';
                            }
                        } else {
                            createHtml += "<h3 style ='margin-top = 20px margin-left = 20px'>暂无文章</h3> "
                        }
                        jQuery("#artListDiv").html(createHtml);
                        
                    }else {
                        alert("查询失败" + res.msg);
                    }
                }
            });
            
            
        }
        // 后端分页返回 当前页面的文章列表 返回最大页码(一共多少页)
        init();
        // function flush(pindexTemp) {
        //     if (pindexTemp < 0) {
        //         alert("参数有误");
        //         return false;
        //     }
        //     pindex = pindexTemp;
        //     jQuery("#pindex").html(pindex);
        //     jQuery.ajax ({
        //         url:"/art/getlistbypage",
        //         type :"GET",
        //         data : {
        //             "psize" : psize,
        //             "pindex" : pindex
        //         },
        //         success : function succ (res) {
        //             if (res.code == 200 && res.data != null) {
        //                 var createHtml = "";
        //                 alert(pindex);
        //                 if(res.data.list != null && res.data.list.size > 0) {
        //                     totalpapge = res.data.size;
        //                     jQuery("#psize").html(totalpapge);
        //                     var artlist = res.data.list;
        //                     for (var i = 0 ; i < res.data.list.size ; i++) {
        //                         art = artlist[i];
        //                         createHtml += '<div class="blog" >';
        //                         createHtml += '<div class="title">' +art.title + '</div>';
        //                         createHtml += '<div class="date">' + art.createtime+'</div>';
        //                         createHtml += '<div class="desc">' + art.content +'</div>';
        //                         createHtml += '<a href="blog_content.html?aid=" ' + art.id + ' class="detail">查看全文 &gt;&gt;</a>';
        //                         createHtml += '</div>';
        //                     }
        //                 } else {
        //                     createHtml += "<h3 style ='margin:auto auto'> 暂无文章</h3>"
        //                 }
        //                 jQuery("#artListDiv").html(createHtml);
        //             } else {
        //                 alert("查询失败" + res.msg);
        //             }
        //         }
        //     });

            
        // }
        function doFirst() {
            if (pindex == 1) {
                alert("当前已在首页");
            } else {
                location.href = "blog_list.html?pindex=" +parseInt(1);
            }
            
        }
        function doBefore() {
            if (pindex == 1) {
                alert("当前已在首页");
            } else {
                location.href = "blog_list.html?pindex=" + (parseInt(pindex)-1);
            }
            
        }

        function doNext() {
            if (pindex == totalpapge) {
                alert("当前已在最后一页");
            } else {
                location.href = "blog_list.html?pindex=" + (parseInt(pindex)+1);
            }
            
        }
        function doLast() {
            if (pindex == totalpapge) {
                alert("当前已在最后一页");
            } else {

                location.href = "blog_list.html?pindex=" + parseInt(totalpapge);
            }   
        }


    </script>
</body>
</html>